package com.lej.core.widget.drawable

import android.content.Context
import android.content.res.ColorStateList
import android.graphics.drawable.Drawable
import android.graphics.drawable.GradientDrawable
import android.graphics.drawable.StateListDrawable
import androidx.annotation.DrawableRes
import com.lej.core.utils.extention.dpf
import com.lej.core.utils.extention.getDrawableCompat
import com.lej.core.utils.extention.resColor

class CustomDrawable(
    shape: Int = RECTANGLE,
    cornerRadius: Int = 0,
    solidColor: Int = android.R.color.transparent,
    strokeWidth: Int = 0,
    strokeColor: Int = android.R.color.transparent,
    leftTopRadius: Int = 0,
    rightTopRadius: Int = 0,
    leftBottomRadius: Int = 0,
    rightBottomRadius: Int = 0,
) : GradientDrawable() {
    init {
        setShape(shape)
        setStroke(strokeWidth, strokeColor.resColor)
        setColor(solidColor.resColor)
        if (cornerRadius != 0) {
            setCornerRadius(cornerRadius.dpf)
        } else {
            cornerRadii = floatArrayOf(
                leftTopRadius.dpf,
                leftTopRadius.dpf,
                rightTopRadius.dpf,
                rightTopRadius.dpf,
                leftBottomRadius.dpf,
                leftBottomRadius.dpf,
                rightBottomRadius.dpf,
                rightBottomRadius.dpf
            )
        }
    }
}

val CHECKED_STATE = intArrayOf(android.R.attr.state_checked)
val SELECTED_STATE = intArrayOf(android.R.attr.state_selected)
val ENABLED_STATE = intArrayOf(android.R.attr.state_enabled)


fun getStateDrawable(
    defdrawable: Drawable,
    selectdrawable: Drawable? = null,
    checkeddrawable: Drawable? = null,
    enableddrawable: Drawable? = null
) = StateListDrawable().apply {
    selectdrawable?.let { addState(SELECTED_STATE, it) }
    enableddrawable?.let { addState(ENABLED_STATE, it) }
    checkeddrawable?.let { addState(CHECKED_STATE, it) }
    addState(intArrayOf(), defdrawable)
}

fun Context.stateDrawable(
    @DrawableRes def:  Int,
    @DrawableRes select: Int? = null,
    @DrawableRes checked: Int? = null,
    @DrawableRes enabled: Int? = null
) = StateListDrawable().apply {
    select?.let { addState(SELECTED_STATE, getDrawableCompat(it)) }
    enabled?.let { addState(ENABLED_STATE, getDrawableCompat(it)) }
    checked?.let { addState(CHECKED_STATE, getDrawableCompat(it)) }
    addState(intArrayOf(), getDrawableCompat(def))
}

fun getStateDrawable(
    defColor: Int? = null,
    selectColor: Int? = null,
    checkedColor: Int? = null,
    enabledColor: Int? = null,
    radius: Int = 0
) = StateListDrawable().apply {
    selectColor?.let {
        addState(SELECTED_STATE, CustomDrawable(solidColor = it, cornerRadius = radius))
    }
    enabledColor?.let {
        addState(ENABLED_STATE, CustomDrawable(solidColor = it, cornerRadius = radius))
    }
    checkedColor?.let {
        addState(CHECKED_STATE, CustomDrawable(solidColor = it, cornerRadius = radius))
    }
    defColor?.let {
        addState(intArrayOf(), CustomDrawable(solidColor = it, cornerRadius = radius))
    }
}


/**创建圆环形状的选择框*/
fun getStateDrawableByRing(
    defColor: Int? = null,
    selectColor: Int? = null,
    checkedColor: Int? = null,
    enabledColor: Int? = null,
    radius: Int = 0,
    width: Int = 1
) = StateListDrawable().apply {
    selectColor?.let {
        addState(
            SELECTED_STATE,
            CustomDrawable(
                solidColor = it,
                shape = GradientDrawable.RING,
                strokeWidth = width,
                cornerRadius = radius
            )
        )
    }
    enabledColor?.let {
        addState(
            ENABLED_STATE,
            CustomDrawable(
                solidColor = it,
                shape = GradientDrawable.RING,
                strokeWidth = width,
                cornerRadius = radius
            )
        )
    }
    checkedColor?.let {
        addState(
            CHECKED_STATE,
            CustomDrawable(
                solidColor = it,
                shape = GradientDrawable.RING,
                strokeWidth = width,
                cornerRadius = radius
            )
        )
    }
    defColor?.let {
        addState(
            intArrayOf(),
            CustomDrawable(
                solidColor = it,
                shape = GradientDrawable.RING,
                strokeWidth = width,
                cornerRadius = radius
            )
        )
    }
}


/**获取多重状态的颜色背景  不可在设置view背景使用*/
fun colorStateList(
    defColor: Int,
    selectColor: Int? = null,
    checkedColor: Int? = null,
    enabledColor: Int? = null
): ColorStateList {
    val list = ArrayList<Pair<IntArray, Int>>()
    selectColor?.let { list.add(SELECTED_STATE to it) }
    enabledColor?.let { list.add(ENABLED_STATE to it) }
    checkedColor?.let { list.add(CHECKED_STATE to it) }
    defColor.let { list.add(intArrayOf() to it) }
    val states = arrayOfNulls<IntArray>(list.size)
    val colors = IntArray(list.size)
    for (i in list.indices) {
        list[i]
        states[i] = list[i].first
        colors[i] = list[i].second.resColor
    }
    return ColorStateList(states, colors)
}


